$(function () {
    //**************************************加载列表轮播图
    let layer = layui.layer;
    let form = layui.form;

    function lunbo() {
        $.ajax({
            url: "/admin/swipers",
            success: function (res) {
                $("tbody").html("");
                $.each(res.data, function (index, item) {
                    console.log(item);
                    if (item.swiperstatus == 1) {
                        let one = $(` <tr>
                    <td>${item.id}</td>
                    <td>
                        <img id="preIcon" src="http://127.0.0.1:8888/uploads/${item.swiperimg}">
                    </td>
                    <td>/aaa/bb/cc/1.jpg</td>
                    <td>
                        <!-- 在使用 -->
                        <span class="layui-badge layui-bg-green">√</span>
                    </td>
                    <td>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-danger delete" _id=${item.id}>
                            删除
                        </button>
                    </td>
                </tr>`);

                        $("tbody").append(one);
                    } else if (item.swiperstatus == 2) {
                        let one = $(` <tr>
                    <td>${item.id}</td>
                    <td>
                        <img src="http://127.0.0.1:8888/uploads/${item.swiperimg}">
                    </td>
                    <td>/aaa/bb/cc/1.jpg</td>
                    <td>
                        <!-- 未使用 -->
                        <span class="layui-badge layui-bg-cyan">×</span>
                    </td>
                    <td>
                        <button type="button" class="layui-btn layui-btn-xs layui-btn-danger delete" _id=${item.id}>
                            删除
                        </button>
                    </td>
                </tr>`);

                        $("tbody").append(one);
                    }
                });
            }
        });
    }
    lunbo()


    $("tbody").on("click", ".delete", function (e) {
        e.preventDefault()
        let id = $(this).attr("_id");
        $.ajax({
            url: "/admin/swipers/" + id,
            type: "DELETE",
            // data: id,
            success: function (res) {
                lunbo()

            }
        });
    })




    // ***************************************上传文件
    $('body').on('click', '#uploadSwiper', function () {
        $('#myfile').click()
    })

    // 监听文件选中事件
    $('body').on('change', '#myfile', function (e) {
        e.preventDefault()
        let files = e.target.files
        var fd = new FormData()
        $.each(files, function (index, item) {
            fd.append('swipers', item)
        })
        $.ajax({
            type: 'post',
            data: fd,
            url: `/admin/swipers`,
            success: function (res) {
                layer.msg(res.message);
                lunbo()
                // console.log(res);
            },
            processData: false, // 不处理数据
            contentType: false, // 不设置内容类型
        });


    })



})